package com.yugao.fintech.antelope.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.yugao.fintech.antelope.base.model.BasePlusPO;
import com.yugao.fintech.antelope.system.mapper.SysNoticeMapper;
import com.yugao.fintech.antelope.system.model.entity.SysNotice;
import com.yugao.fintech.antelope.system.service.SysNoticeService;
import com.yugao.fintech.framework.core.page.PageModel;
import com.yugao.fintech.framework.core.page.TableSupport;
import com.yugao.fintech.framework.mybatisplus.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SysNoticeServiceImpl implements SysNoticeService {
    @Autowired
    private SysNoticeMapper noticeMapper;

    /**
     * 查询公告信息
     *
     * @param noticeId 公告ID
     * @return 公告信息
     */
    @Override
    public SysNotice getById(Long noticeId) {
        return noticeMapper.selectById(noticeId);
    }

    @Override
    public IPage<SysNotice> pageNotice(SysNotice rq) {
        PageModel pageModel = TableSupport.getPageModel();
        LambdaQueryWrapper<SysNotice> lqw = getLqw(rq);
        IPage<SysNotice> pageRq = PageUtil.to(pageModel);
        return noticeMapper.selectPage(pageRq, lqw);
    }

    /**
     * 查询公告列表
     *
     * @param rq 公告信息
     * @return 公告集合
     */
    @Override
    public List<SysNotice> list(SysNotice rq) {
        return noticeMapper.selectList(getLqw(rq));
    }

    private LambdaQueryWrapper<SysNotice> getLqw(SysNotice rq) {
        return SysNotice.lqw()
                .like(StringUtils.isNotEmpty(rq.getNoticeTitle()), SysNotice::getNoticeTitle, rq.getNoticeTitle())
                .eq(StringUtils.isNotEmpty(rq.getNoticeType()), SysNotice::getNoticeType, rq.getNoticeType())
                .like(StringUtils.isNotEmpty(rq.getCreatedBy()), BasePlusPO::getCreatedBy, rq.getCreatedBy());
    }

    /**
     * 新增公告
     *
     * @param notice 公告信息
     * @return 结果
     */
    @Override
    public boolean saveNotice(SysNotice notice) {
        return noticeMapper.insert(notice) > 0;
    }

    /**
     * 修改公告
     *
     * @param notice 公告信息
     * @return 结果
     */
    @Override
    public boolean updateNotice(SysNotice notice) {
        return noticeMapper.updateById(notice) > 0;
    }

    /**
     * 删除公告对象
     *
     * @param noticeId 公告ID
     * @return 结果
     */
    @Override
    public boolean deleteNotice(Long noticeId) {
        return noticeMapper.deleteById(noticeId) > 0;
    }

    /**
     * 批量删除公告信息
     *
     * @param noticeIds 需要删除的公告ID
     * @return 结果
     */
    @Override
    public boolean deleteBatchNotice(Long[] noticeIds) {
        return noticeMapper.deleteBatchIds(Arrays.stream(noticeIds).collect(Collectors.toList())) > 0;
    }
}
